﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Entity.Base_Leasesystem_equipment;
using Coldairarrow.Entity.ViewModel;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_Leasesystem_equipment
{
    public class leasesystem_equipmentBusiness : BaseBusiness<leasesystem_equipment>, Ileasesystem_equipmentBusiness, ITransientDependency
    {
        public leasesystem_equipmentBusiness(IDbAccessor db, IHomeBusiness homeBusiness)
            : base(db)
        {
            _homebusiness = homeBusiness;
        }
        IHomeBusiness _homebusiness { get; set; }
        #region 外部接口

        public async Task<PageResult<leasesystem_equipment>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_equipment>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_equipment, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<leasesystem_equipment> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_equipment data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(leasesystem_equipment data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            for (int i = 0; i < ids.Count; i++)
            {
                var entity = GetEntity(ids[i]);
                if (!entity.VehiclelicensePhoto.IsNullOrEmpty()) {
                    string VehiclelicensePhoto = entity.VehiclelicensePhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{VehiclelicensePhoto}");
                }
                if (!entity.BuyDeviceInvoicePhoto.IsNullOrEmpty())
                {
                    string BuyDeviceInvoicePhoto = entity.BuyDeviceInvoicePhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{BuyDeviceInvoicePhoto}");
                }
                if (!entity.CompulsoryinsurancePhoto.IsNullOrEmpty())
                {
                    string CompulsoryinsurancePhoto = entity.CompulsoryinsurancePhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{CompulsoryinsurancePhoto}");
                }
                if (!entity.CommercialinsurancePhoto.IsNullOrEmpty())
                {
                    string CommercialinsurancePhoto = entity.CommercialinsurancePhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{CommercialinsurancePhoto}");
                }
                if (!entity.DevicefrontPhoto.IsNullOrEmpty())
                {
                    string DevicefrontPhoto = entity.DevicefrontPhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{DevicefrontPhoto}");
                }
                if (!entity.DevicesidePhoto.IsNullOrEmpty())
                {
                    string DevicesidePhoto = entity.DevicesidePhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{DevicesidePhoto}");
                }
                if (!entity.DeviceNameplate.IsNullOrEmpty())
                {
                    string DeviceNameplate = entity.DeviceNameplate.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{DeviceNameplate}");
                }
                if (!entity.DeviceCertification.IsNullOrEmpty())
                {
                    string DeviceCertification = entity.DeviceCertification.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{DeviceCertification}");
                }
                await DeleteAsync(ids[i]);
            }
            
        }

        public List<SelectModel> FullSelect()
        {
            List<SelectModel> list = new List<SelectModel>();
            var a = Db.GetIQueryable<leasesystem_equipment>().Select(a=>new { 
            value = a.Id,
            text = a.PurchasingPrice
            }).ToList();

            foreach (var item in a)
            {
                SelectModel s = new SelectModel();
                s.value = item.value;
                s.text = item.text;
                list.Add(s);
            }

            return list;
        }

        public async Task<object> GetHeightSearch(PageInput<ConditionDTO> input)
        {
            var q = Db.GetIQueryable<leasesystem_equipment>();
            var newWhere = q;


            if (!input.Search.Keyword.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.DeviceNo.Contains(input.Search.Keyword));
            }
            if (!input.Search.EmpName.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.EmpName.Contains(input.Search.EmpName));
            }

            if (!input.Search.Status.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.Status == int.Parse(input.Search.Status));
            }
            if (!input.Search.CarBrandModel.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.DeviceBrandModel == input.Search.CarBrandModel);
            }
            var Data = new
            {
                Data = newWhere.Skip((input.PageIndex - 1) * input.PageRows)
               .Take(input.PageRows),
                Total = q.Count()
            };
            return Data;

        }
        #endregion

        #region 私有成员

        #endregion
    }
}